/* beautify ignore:start */
import {Directive, ElementRef, Renderer, EventEmitter} from '@angular/core';
import {ListWrapper} from '../../common';
/* beautify ignore:end */

@Directive({
    selector: '[wm-sort]',
    inputs: ['field: wm-sort'],
    outputs: ['sortChanged'],
    host: {
        '(click)': 'onClick()'
    }
})
export class TableSort {

    public field: string;   // 排序的属性
    public sortChanged: EventEmitter<any> = new EventEmitter();
    private sort: string;

    constructor(private el: ElementRef, private renderer: Renderer) {
        el.nativeElement.className = 'has-sort';
    }

    removeClass() {
        this.el.nativeElement.className = 'has-sort ';
    }

    onClick() {
        switch (this.sort) {
            case 'asc':
                this.sort = 'desc';
                break;
            case 'desc':
                this.sort = '';
                break;
            default:
                this.sort = 'asc';
                break;
        }
        this.el.nativeElement.className = 'has-sort ' + this.sort;
        this.sortChanged.emit({field: this.field, sort: this.sort});
    }

}